import vue from '@vitejs/plugin-vue'
import { resolve } from 'node:path'
import { fileURLToPath, URL } from 'node:url'
import AutoImport from 'unplugin-auto-import/vite'
import UnpluginSvgComponent from 'unplugin-svg-component/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { defineConfig, loadEnv } from 'vite'
import vueDevTools from 'vite-plugin-vue-devtools'
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  return {
    base: './',
    plugins: [
      vue(),
      vueDevTools(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      UnpluginSvgComponent({
        iconDir: [resolve(fileURLToPath(new URL('./src/assets/icons', import.meta.url)))],
        preserveColor: resolve(
          fileURLToPath(new URL('./src/assets/icons/preserve-color', import.meta.url)),
        ),
        dts: true,
        prefix: 'icon',
        dtsDir: resolve(fileURLToPath(new URL('./types/auto', import.meta.url))),
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    server: {
      host: '0.0.0.0',
      open: true,
      proxy: {
        [env.VITE_BASE_URL_PREFIX]: {
          target: env.VITE_BASE_URL,
          rewrite: (path) => path.replace(new RegExp('^' + env.VITE_BASE_URL_PREFIX), ''),
          changeOrigin: true,
          ws: false,
        },
      },
    },
  }
})
